package com.aluvfy.jdbc;

import com.aluvfy.jdbc.utils.DbUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/*使用preparedStatement完成分页查询*/
public class JDBCTest14 {
    public static void main(String[] args) {
        /*查询所有员工姓名，每页显示三条（pageSize），显示第二页（pageNo）*/
        //每页显示的条数
        int pageSize = 3;
        //显示第几页
        int pageNo = 2;

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DbUtils.getConnection();
            String sql = "SELECT name FROM t_employee LIMIT ?,?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, (pageNo - 1) * pageSize);
            ps.setInt(2, pageSize);
            rs = ps.executeQuery();
            while (rs.next()){
                String nam = rs.getString("name");
                System.out.println(nam);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DbUtils.close(conn, ps, rs);
        }
    }
}
